package com.flyme.module.base.provider.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.core.base.supper.controller.BaseController;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.module.base.client.api.SysScorecheckRemoteService;
import com.flyme.module.base.client.dto.SysScorcheckDto;
import com.flyme.module.base.client.entity.SysScorecheck;
import com.flyme.module.base.provider.service.SysScorecheckService;
import com.flyme.module.base.provider.service.SysUsercertificateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RestController;

/**
 * 学分转换审核管理
 * @author zyh
 * @date 2020-4-8
 */
@Api(tags = "学分转换审核")
@RestController
public class SysScorecheckController extends BaseController<SysScorecheckService, SysScorecheck> implements SysScorecheckRemoteService {

    @Autowired
    private SysUsercertificateService usercertificateService;

    @ApiOperation(value = "获取学分转换申请", notes = "获取学分转换申请")
    @Override
    public ResultBody list(@ModelAttribute(value = "pagerInfo") PagerInfo pagerInfo, SysScorcheckDto scorecheck ) {
        CriteriaQuery cq = new CriteriaQuery<EntityMap>(pagerInfo);
        cq.select("s.*,user.userName,user.schoolNum,certificate.certificateName,certificate.discipline,certificate.score");
        cq.like("user.userName",scorecheck.getUserName());
        //cq.eq("s.checkStatus", 2);
        cq.createAlias("user", "user");
        cq.createAlias("usercertificate", "usercertificate");
        PqGrid pqGrid = bizService.pageList(cq);
        return ResultBody.success(pqGrid);
    }

    @ApiOperation(value = "编辑课程", notes = "编辑课程")
    @Override
    public ResultBody edit(SysScorecheck scorecheck) {
        //审核通过，将用户证书改为已兑换
        if(scorecheck.getCheckStatus().equals(1)){
            scorecheck.setRefuseCause("");
            UpdateWrapper u = new UpdateWrapper();
            usercertificateService.update(u.set(true, "isConvert", 1).eq(true, "id", scorecheck.getUsercertificateId()));
        }
        boolean result= bizService.saveOrUpdate(scorecheck);
        return ResultBody.success(result);
    }
}
